package com.CST.service.impl.ys;

import com.CST.dto.ys.SubjectSortDTO;
import com.CST.mapper.cst.ys.YsSubjectSortMapper;
import com.CST.service.ys.YsSubjectSortService;
import com.CST.vo.ys.SubjectSortVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author cw
 */
@Service
public class YsSubjectSortServiceImpl implements YsSubjectSortService {

    @Autowired
    private YsSubjectSortMapper mapper;

    @Override
    public List<SubjectSortVO> getSubjectSortList(Integer type) {
        List<SubjectSortVO> filteredList = new ArrayList<>();
        List<SubjectSortVO> list ;
        if (type == 5){
            // 业务职能
            list = mapper.selectBusinessList();
        }else{
            // 管理职能
            list =mapper.selectManageList();
        }
        // 筛选list中sort为NULL的值,list中第一条记录为sort是最大的
        if (list.size() == 0){
            return list;
        }
        List<Long> subjectIds = mapper.selectNowList(type);
        // 筛选出排序表中多余的数据，删除掉
        List<Long> subjectIdList = list.stream().map(SubjectSortVO::getSubjectId).collect(Collectors.toList());
        List<Long> removeSubjectIds = subjectIds.stream().filter(item -> !subjectIdList.contains(item)).collect(Collectors.toList());
        for(Long subjectId : removeSubjectIds){
            deleteSort(subjectId,type);
        }
        Integer maxSort = mapper.selectMaxSortIndex(type);
        // 查询出sort表中已经有的排序数据，筛选出来后，将没有排序的数据插入进去排序表
        Set<Long> subjectIdsSet = new HashSet<>(subjectIds);
        for (SubjectSortVO subjectSortVO : list) {
            if (!subjectIdsSet.contains(subjectSortVO.getSubjectId())) {
                maxSort= maxSort == null ? 1:maxSort + 1;
                subjectSortVO.setSortIndex(maxSort);
                filteredList.add(subjectSortVO);
            }
        }
        if (filteredList.size()>0){
            mapper.insertNewSubjects(filteredList,type);
        }else{
            return list;
        }
        // 排序表更新后重新查询一次返回数据
        if (type == 5){
            // 业务职能
            return mapper.selectBusinessList();
        }else{
            // 管理职能
            return mapper.selectManageList();
        }
    }

    @Override
    public boolean updateSortIndex(SubjectSortDTO dto) {
        try{
            // 自下而上
            if (dto.getStartIndex() > dto.getEndIndex()){
                //endIndex <= x < startIndex 依次加1
                mapper.updateDownwardSortIndex(dto);
            }
            // 自上向下
            if (dto.getStartIndex() < dto.getEndIndex()){
                // startIndex < x <= endIndex 依次减1
                mapper.updateUpwardSortIndex(dto);
            }
            // startIndex = endIndex
            mapper.updateSortIndexBySubjectId(dto);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    /**
     * 删除预算科目的时候需要减科目排序修改
     */
    private void deleteSort(Long subjectId,Integer type) {
        // 查询预算科目的排序
        Integer sortIndex = mapper.selectSortBySubjectIdAndType(subjectId, type);
        // 删除预算科目的排序，业务职能和管理职能
        mapper.deleteBySubjectIdAndType(subjectId,type);
        // 修改被影响到的预算科目排序
        mapper.updateSortIndex(sortIndex,type);
    }
}
